﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using Wizard.UI;

namespace AppScan
{
    public partial class FieldsPage : InternalWizardPage
    {
        /************
         * TO DO:
         *   Import SCan options bools
         *   Import the ability to retrieve 
         * information from list items.
         * ************/

        private readonly CreateExportContext _context;
        //private string[] default_txt = { "\\hostname or IPv4 address", " (i.e 192.168.1.55)", "(i.e 192.168.1.230)", "\\DOMAIN (or \\sub.domain.top)", "Select a file to open  ->" };
        public FieldsPage(CreateExportContext context)
        {
            _context = context;
            InitializeComponent();
        }

        private void UserNamePage_SetActive(object sender, CancelEventArgs e)
        {
            EnableControls();
        }

        private void EnableControls()
        {
            bool nextEnabled = true;

            if (dataGridView1.RowCount < 2)
                nextEnabled = false;

            if (nextEnabled)
                SetWizardButtons(WizardButtons.Back | WizardButtons.Next);
            else
                SetWizardButtons(WizardButtons.Back);
        }

        private void userNameTextBox_TextChanged(object sender, EventArgs e)
        {
            EnableControls();
        }

        private void fullNameTextBox_TextChanged(object sender, EventArgs e)
        {
            EnableControls();
        }

        private void UserNamePage_WizardNext(object sender, WizardPageEventArgs e)
        {
            try
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if ((dataGridView1.Rows[i].Cells[0].Value as CheckBox).Checked == true)
                    {
                        _context.fields.Add(dataGridView1.Rows[i].Cells[1].Value.ToString());
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error selecting fields!");
            }

            //First, check to if list is empty
            if (_context.fields.Count < 1)
            {
                MessageBox.Show("No targets import\nPlease import targets to scan.");
            }
            else
            {
                e.NewPage = "ReviewPage";
            }
        }

        private void Banner_Load(object sender, EventArgs e)
        {

        }

        private void fix_spacing()
        {





        }

        private void open_file_btn_Click(object sender, EventArgs e)
        {
            //Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            string path = Directory.GetCurrentDirectory();
            openFileDialog1.InitialDirectory = path;
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    //if ((myStream = openFileDialog1.OpenFile()) != null)
                    //{
                    //    using (myStream)
                    //    {
                    //        // Insert code to read the stream here.
                    //    }
                    //}
                    filename_txtBox.Text = openFileDialog1.FileName;

                }
                catch (Exception ex)
                {
                    // MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        //private void singleTargetIP_box_Click(object sender, EventArgs e)
        //{
        //    foreach (string str in default_txt)
        //    {
        //        if ((sender as TextBox).Text.ToString().ToLower().Contains(str.ToLower()))
        //        {
        //            (sender as TextBox).ForeColor = System.Drawing.Color.Black;
        //            (sender as TextBox).Font = new System.Drawing.Font((sender as TextBox).Font.Name, (sender as TextBox).Font.Size, System.Drawing.FontStyle.Regular, (sender as TextBox).Font.Unit);

        //            (sender as TextBox).Text = "";
        //        }
        //    }
        //}

        //private void remove_btn_Click(object sender, EventArgs e)
        //{
        //    for (int i = 0; i < dataGridView1.Rows.Count; i++)
        //    {
        //        System.Diagnostics.Debug.WriteLine(dataGridView1.Rows[i].Cells[0].Value);
        //        if (dataGridView1.Rows[i].Cells[0].Selected)
        //        {
        //            dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
        //        }
        //    }
        //    EnableControls();
        //}

        //private void ClearALL_btn_Click(object sender, EventArgs e)
        //{
        //    DialogResult result;
        //    result = MessageBox.Show("Are you sure you want to clear targets dataGridView1?", "Clear Targets?", MessageBoxButtons.YesNo);
        //    if (result == DialogResult.No)
        //    {
        //        MessageBox.Show("Targets NOT cleared.");
        //    }
        //    if (result == DialogResult.Yes)
        //    {
        //        dataGridView1.Rows.Clear();
        //        //for (int i = dataGridView1.Rows.Count - 2; i >= 0; i--)
        //        //{
        //        //    System.Diagnostics.Debug.WriteLine(dataGridView1.Rows[i].Cells[0].Value);

        //        //    if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()))
        //        //    {

        //        //    }
        //        //    else
        //        //    {

        //        //        dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
        //        //    }
        //        //}
        //    }
        //}

        private void FieldsPage_Load(object sender, EventArgs e)
        {
            // customize dataviewgrid, add checkbox column
            DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
            checkboxColumn.Width = 30;
            checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns.Insert(0, checkboxColumn);

            // add checkbox header
            System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
            // set checkbox header to center of header cell. +1 pixel to position correctly.
            rect.X = rect.Location.X + (rect.Width / 4);

            CheckBox checkboxHeader = new CheckBox();
            checkboxHeader.Name = "checkboxHeader";
            checkboxHeader.Size = new System.Drawing.Size(18, 18);
            checkboxHeader.Location = rect.Location;
            checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
            
            dataGridView1.Controls.Add(checkboxHeader);
        }

        private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1[0, i].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
            }
            dataGridView1.EndEdit();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            int i = 0;
            foreach (Control ctrl in this.splitContainer1.Panel1.Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    if ((ctrl as CheckBox).Checked)
                    {
                        if ((ctrl as CheckBox).Name.Contains("applicationitem"))
                        {
                            ApplicationList.ApplicationItem ai = new ApplicationList.ApplicationItem();
                            Type t = ai.GetType();
                            foreach (System.Reflection.PropertyInfo p in t.GetProperties())
                            {
                                dataGridView1.Rows[i].Cells[0].Value = false;
                                dataGridView1.Rows[i].Cells[1].Value = p.Name;
                                i++;
                            }

                        }
                        if ((ctrl as CheckBox).Name.Contains("qfe"))
                        {
                            UpdateList.WindowsUpdate wu = new UpdateList.WindowsUpdate();
                            Type t = wu.GetType();
                            foreach (System.Reflection.PropertyInfo p in t.GetProperties())
                            {
                                dataGridView1.Rows[i].Cells[0].Value = false;
                                dataGridView1.Rows[i].Cells[1].Value = p.Name;
                                i++;
                            }

                        }
                        if ((ctrl as CheckBox).Name.Contains("osystem"))
                        {
                            OSList.OSystem os = new OSList.OSystem();
                            Type t = os.GetType();
                            foreach (System.Reflection.PropertyInfo p in t.GetProperties())
                            {
                                dataGridView1.Rows[i].Cells[0].Value = false;
                                dataGridView1.Rows[i].Cells[1].Value = p.Name;
                                i++;
                            }

                        }
                        if ((ctrl as CheckBox).Name.Contains("reg"))
                        {
                            registry reg = new registry();
                            Type t = reg.GetType();
                            foreach (System.Reflection.PropertyInfo p in t.GetProperties())
                            {
                                dataGridView1.Rows[i].Cells[0].Value = false;
                                dataGridView1.Rows[i].Cells[1].Value = p.Name;
                                i++;
                            }

                        }
                    }
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            EnableControls();
        }
    }
}
